package com.army.study.common;

import com.army.study.utils.LogUtils;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

/**
 * 许可
 */
public class PermissionBridge {

    // 已授予权限处理程序
    public static final int EVENT_PERMISSION_GRANTED = 0X0000023;
    // 拒绝权限处理程序
    public static final int EVENT_PERMISSION_DENIED = 0X0000024;

    private static final String TAG = PermissionBridge.class.getSimpleName();

    private static OnPermissionStateListener onPermissionStateListener;

    public void setOnPermissionStateListener(OnPermissionStateListener permissionStateListener) {
        onPermissionStateListener = permissionStateListener;
    }

    private static EventHandler handler = new EventHandler(EventRunner.current()) {
        @Override
        protected void processEvent(InnerEvent event) {
            switch (event.eventId) {
                case EVENT_PERMISSION_GRANTED:
                    onPermissionStateListener.onPermissionGranted();
                    break;
                case EVENT_PERMISSION_DENIED:
                    onPermissionStateListener.onPermissionDenied();
                    break;
                default:
                    LogUtils.info(TAG, "EventHandler Undefined Event");
            }
        }
    };
    public static EventHandler getHandler() {
        return handler;
    }

    public interface OnPermissionStateListener {

        /**
         * 当同意授权时触发
         */
        void onPermissionGranted();

        /**
         * 当拒绝授权时触发
         */
        void onPermissionDenied();

    }
}
